package com.ctbri.gui;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.ctbri.util.SwingConsole;

class Point1 {
	int x;
	int y;
}

class CurvePanel extends JPanel {
	private static int totalCount = 20;
	private List<Point1> points = new ArrayList<>();
	private int result = 0;

	private void drawLine(Graphics g) {
		if(points.size() == 0) {
			Point1 point = new Point1();
			point.x = 0;
			point.y = getHeight();
			points.add(point);
			return;
		}
		Point1 tmp = null;
		for(int i = 0; i < points.size(); i++) {
			if(tmp == null) {
				tmp = points.get(i);
				continue;
			}
			Point1 point = points.get(i);
			g.drawLine(tmp.x, tmp.y, point.x, point.y);
			tmp = point;
		}
	}
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		drawLine(g);
	}
	
	public void setResult(int result) {
		this.result = result;
		if(points.size() == totalCount){
			Point1 tmp = points.get(totalCount - 1);
			tmp.x = 0;
			points.clear();
			points.add(tmp);
		}
		
		int widthStep = getWidth() / 20;
		int heightStep = getHeight() / 30;
		
		Point1 point = new Point1();
		point.x = widthStep * points.size();
		point.y = result * heightStep;
		points.add(point);
		repaint();
	}
}
public class Ex28 extends JFrame {
	private JButton button = new JButton("button");
	private CurvePanel panel = new CurvePanel();
	private Random random = new Random();
	public Ex28() {
		add(panel);
		add(button, BorderLayout.SOUTH);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int result = random.nextInt(6) + random.nextInt(6) + random.nextInt(6) + random.nextInt(6) + random.nextInt(6) +5;
				panel.setResult(result);
			}
		});
	}
	public static void main(String[] args) {
		SwingConsole.run(new Ex28(), 600, 600);
	}

}
